package com.cy.pj.notice.web.interceptor;

import com.cy.pj.common.exception.ServiceException;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

public class TimeAccessInterceptor implements HandlerInterceptor {


    /**
     * 后端handler方法执行之前执行
     * @param request 请求对象
     * @param response 响应对象
     * @param handler 目标处理对象
     * @return 表示是否对请求放行，是否继续执行
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("==preHandle==");
        //基于jdk8中
        LocalTime localTime=LocalTime.now();
        int hour=localTime.getHour();
        System.out.println("hour="+hour);
        if(hour<9||hour>18)
            throw new  ServiceException("请求指定时间访问");
        return true;//true表示放行，false表示请求到此结束
    }

}
